﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using HighFantasyRPG;

namespace MapEditor
{
    public partial class Form1 : Form
    {
        private Graphics graphics, BBG;
        private Bitmap bb;
        private DirectoryInfo directory;
        private string[,] map, sprs;
        private Rectangle Srect, drect;
        private Map mapfile;
        private int mouseX, mouseY, CurrentX, CurrentY, selectedtile;
        
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            graphics = pictureBox1.CreateGraphics();
            bb = new Bitmap(640, 480);
            LoadLevelsList();


            map = new string[20, 15];
            sprs = new string[20, 15];

            for (int y = 0; y <= 14; y++)
            {

                for (int x = 0; x <= 19; x++)
                {
                    map[x, y] = "space";
                    sprs[x, y] = "nothing";


                }

            }


            timer1.Enabled = true;
            timer1.Start();

            
 
        }

        private void LoadLevelsList()
         {
	            listBox1.Items.Clear();

	            directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Content\\Maps");


	            foreach (FileInfo file in directory.GetFiles()) {
                    

		        listBox1.Items.Add(file.Name);

	                  }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int count = 1;
            count++;
            label4.Text = count.ToString();
            DrawBlocks();
            
        }


        private void DrawBlocks()
        {
            

            for (int x = 0; x <= 19; x++)
            {

                for (int y = 0; y <= 14; y++)
                {

                    Srect = new Rectangle(x * 32, y * 32, 32, 32);


                    switch (map[x, y])
                    {

                        case "space":



                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._0, drect);

                            break;


                        case "grass":


                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._1, drect);

                            break;

                        case "water":

                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._2, drect);

                            break;
                        case "rock":

                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._3, drect);

                            break;
                        case "dirt":

                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._5, drect);

                            break;
                        

                    }

                    switch (sprs[x, y])
                    {

                        case "player":

                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._5, drect);

                            break;
                        case "enemy":


                            drect = new Rectangle((x * 32), (y * 32), 32, 32);
                            graphics.DrawImage(Properties.Resources._5, drect);


                            break;

                        default:

                            break;

                    }


                    graphics.DrawRectangle(Pens.Red, (mouseX * 32), (mouseY * 32), 32, 32);
                }
            }
            // COPY BACKBUFFER TO GRAPHICS OBJECT
            graphics = Graphics.FromImage(bb);

            // DRAW BACKBUFFER TO SCREEN
            BBG = this.pictureBox1.CreateGraphics();
            BBG.DrawImage(bb, 0, 0, 640, 500);

            graphics.Clear(Color.Black);


        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {

            richTextBox1.Clear();
            this.openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            this.openFileDialog1.Filter = "Level xml file (*.xml)|*.xml|All Files (*.*)|*.*";
            this.openFileDialog1.FilterIndex = 1;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {

                this.textBox1.Text = openFileDialog1.FileName;
                string filepath = openFileDialog1.FileName;
                Stream file = File.Open(filepath, FileMode.Open);
                XmlSerializer ax = new XmlSerializer(typeof(Map));
                mapfile = (Map)ax.Deserialize(file);


                richTextBox1.AppendText("<Map>" + Environment.NewLine);
                richTextBox1.AppendText("<Rows>" + Environment.NewLine);
                richTextBox1.AppendText("<Row>" + Environment.NewLine);
                richTextBox1.AppendText("<columns>" + Environment.NewLine);

                }

            int with= mapfile.rows[0].columns.Count();
            int height = mapfile.rows.Count();

            map = new string[with, height];
            sprs = new string[with, height];


            for (int r = 0; r <= mapfile.rows.Count() - 1; r++)
            {
                


                for (int i = 0; i <= mapfile.rows[r].columns.Count() - 1; i++)
                {
                    

                    map[i, r] = Enum.GetName(typeof(Enumeration.TileType), mapfile.rows[r].columns[i].tileType);
                    sprs[i, r] = Enum.GetName(typeof(Enumeration.SpriteType), mapfile.rows[r].columns[i].spriteType);
                    

                    richTextBox1.AppendText("<Column>" + Environment.NewLine);



                    if ((sprs[i, r] != null))
                    {


                        richTextBox1.AppendText("<spriteType>" + sprs[i, r] + "</spriteType>" + Environment.NewLine);

                    }


                    richTextBox1.AppendText("<tileType>" + map[i, r] + "</tileType>" + Environment.NewLine);
                    richTextBox1.AppendText("</Column>" + Environment.NewLine);

                }


                richTextBox1.AppendText("</columns>" + Environment.NewLine);
                richTextBox1.AppendText("</Row>" + Environment.NewLine);
                richTextBox1.AppendText("</rows>" + Environment.NewLine);

                richTextBox1.AppendText("</Map>" + Environment.NewLine);


            }

        }

        private void pictureBox1_MouseMove(Object sender, System.Windows.Forms.MouseEventArgs e) 
        {
            double X = e.X;
            double Y = e.Y;

        mouseX = (int)Math.Floor(X / 32);

        mouseY = (int)Math.Floor(Y / 32);


        label2.Text = mouseX.ToString();
        label3.Text = mouseY.ToString();

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            CurrentX = mouseX;
            CurrentY = mouseY;
            label5.Text = CurrentX.ToString();
            label6.Text = CurrentY.ToString();

            switch (selectedtile)
                {

                case 0:

                        map[CurrentX, CurrentY] = "space";
                        break;


                case 1:

                        map[CurrentX, CurrentY] = "grass";
                        break;

                }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            selectedtile = 0;
            label7.Text = "Space";
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            selectedtile = 1;
            label7.Text = "Grass";
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            this.saveFileDialog1.Filter = "Level xml file (*.xml)|*.xml|All Files (*.*)|*.*";
            this.saveFileDialog1.FilterIndex = 1;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                {

                    

                        

                for (int y = 0; y <= 14; y++)
                   {

                for (int x = 0; x <= 19; x++)
                {
                    mapfile = new Map(){
                        rows = new Row[15],
                    };
                    mapfile.rows[y] = new Row();
                    
                    mapfile.rows[y].columns[x] = new Column() { 
                    collision = Enumeration.Collision.tile,
                    tileType = (Enumeration.TileType)Enum.Parse(typeof(Enumeration.TileType), map[x, y]),
                    spriteType = (Enumeration.SpriteType)Enum.Parse(typeof(Enumeration.SpriteType), sprs[x, y]),
                    variation = 1
                    };

                    

                }
                   }
            

                    mapfile.Serialize(saveFileDialog1.FileName, mapfile);


                }
        }

    }
}
